package org.example.springboot.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.example.springboot.common.Result;
import org.example.springboot.entity.Book;
import org.example.springboot.service.CollectionService;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 收藏控制器
 */
@Tag(name="收藏管理接口")
@RestController
@RequestMapping("/collection")
public class CollectionController {
    @Resource
    private CollectionService collectionService;
    
    @Operation(summary = "添加收藏")
    @PostMapping("/add")
    public Result<?> addCollection(@RequestBody Map<String, Long> params) {
        Long bookId = params.get("bookId");
        if (bookId == null) {
            return Result.error("图书ID不能为空");
        }
        
        Long collectionId = collectionService.addCollection(bookId);
        return Result.success("收藏成功", collectionId);
    }
    
    @Operation(summary = "取消收藏")
    @PostMapping("/cancel")
    public Result<?> cancelCollection(@RequestBody Map<String, Long> params) {
        Long bookId = params.get("bookId");
        if (bookId == null) {
            return Result.error("图书ID不能为空");
        }
        
        collectionService.cancelCollection(bookId);
        return Result.success("取消收藏成功");
    }
    
    @Operation(summary = "检查是否已收藏")
    @GetMapping("/check")
    public Result<?> isCollected(@RequestParam Long bookId) {
        boolean collected = collectionService.isCollected(bookId);
        return Result.success(collected);
    }
    
    @Operation(summary = "获取用户收藏列表")
    @GetMapping("/list")
    public Result<?> getUserCollections(
            @RequestParam(defaultValue = "1") Integer currentPage,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<Book> page = collectionService.getUserCollections(currentPage, size);
        return Result.success(page);
    }
}